package com.mediacloud.app.newsmodule.activity.military_encyclopedia

import android.app.Dialog
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.utils.ToastUtil


class ErrorCorrectingDialog(context:Context):Dialog(context, R.style.MBottomSheetDialog),
    View.OnClickListener {

    var mContext = context
    var etCorrectContent: EditText? = null
    var recyclerView: RecyclerView? = null
    var tvUpNum: TextView? = null
    var tvBottomSubmit: TextView? = null
    init {
        setContentView(R.layout.dialog_wiki_correct_error)
        recyclerView = findViewById(R.id.recyclerView)
        tvUpNum = findViewById(R.id.tvUpNum)
        etCorrectContent = findViewById(R.id.etCorrectContent)
        tvBottomSubmit = findViewById<TextView>(R.id.tvBottomSubmit)

        tvBottomSubmit?.setOnClickListener(this)
        etCorrectContent?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                notifySubmit()
            }

        })
    }


    override fun show() {
        super.show()
        val window = window
        window?.setGravity(Gravity.BOTTOM)
        val params = window!!.attributes
        params.width = -1
        window.attributes = params
    }

    override fun onClick(v: View) {
        when(v.id){

            R.id.tvBottomSubmit->{
                etCorrectContent?.let{
                    if (it.text.isEmpty()){
                        ToastUtil.showToastCenter(v.context,"请输入内容")
                    }else{
                        callBack?.submit(it.text.toString())
                    }

                }

            }
        }
    }

    fun notifySubmit() {
        if (etCorrectContent?.text.isNullOrEmpty()) {
            tvBottomSubmit?.setBackgroundResource(R.drawable.shape_80d43d3d_r4)
        } else {
            tvBottomSubmit?.setBackgroundResource(R.drawable.shape_d43d3d_r4)
        }

    }

    interface  ItemClickListener{
        fun submit(content:String)
    }
    var callBack:ItemClickListener ?= null
    fun setOnCallBack(callBack:ItemClickListener){
        this.callBack = callBack
    }



}